<?php


namespace app\api\controller;


use think\Config;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Colony extends Controller
{

    //获取社区banner
    public function findSlide(){
        try{
            $where['colony_id'] = $_POST['colony_id'];
            $list = Db::name("system_slide")
                ->where($where)->field("id,name,path,link")
                ->order("sort desc,id desc")
                ->limit(4)->select();

            $preg = "/^http(s)?:\\/\\/.+/";
            foreach ($list as $k => $v) {

                if(!empty($v['path'])){
                    if(!preg_match($preg,$v['path']))
                    {
                        $list[$k]['path'] = "https://" . $_SERVER['HTTP_HOST'] . $v['path'];
                    }
                }

                //$list[$k]['path'] = empty($v['path']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" : "https://" . $_SERVER['HTTP_HOST'] . $v['path'];
            }
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['data'] = $list;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = "获取失败";
        }
        return json_encode($black);
    }


    //获取所在社区
    public function findColony()
    {
        try {
            $common = new Common();
            $userid = $common->common();
            $where["e.id"] = $userid;
            $list = Db::name("system_colony")->alias("a")->join("system_colony_community b","a.id = b.colony_id")
                ->join("system_community_housing c","b.community_id = c.community_id")
                ->join("system_owner d","c.id = d.housing_id")
                ->join("system_fans e","d.fans_id = e.id")
                ->where($where)
                ->group("a.id")
                ->field("a.id,a.name")
                ->select();
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['data'] = $list;
            return json_encode($black);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = "获取失败";
            return json_encode($black);
        }
    }

    //获取社区导航栏目
    public function nav()
    {
        try {
            $where['colony_id'] = $_POST['colony_id'];
            $list = Db::name("system_nav")
                ->where($where)->field("id,name,logo,type")
                ->order("sort desc,id desc")
                ->limit(8)->select();
            $preg = "/^http(s)?:\\/\\/.+/";
            foreach ($list as $k => $v) {
                if(!empty($v['logo'])){
                    if(!preg_match($preg,$v['logo']))
                    {
                        $list[$k]['logo'] = "https://" . $_SERVER['HTTP_HOST'] . $v['logo'];
                    }
                }
                //$list[$k]['logo'] = empty($v['logo']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" : "https://" . $_SERVER['HTTP_HOST'] . $v['logo'];
            }
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['data'] = $list;
            return json_encode($black);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = "获取失败";
            return json_encode($black);
        }
    }
    //获取子栏目
    public function subtopic(){
        try {
            $where['nav_id'] = $_POST['nav_id'];
            $where['pid'] = 0;
            $where['status'] = 1;
            $list = Db::name("system_article_classify")
                ->where($where)->field("id,name,type,thumb")
                ->order("sort desc,id desc")
                ->select();
            //获取下级栏目
            foreach ($list as $k=>$v){
                $where1['pid'] = $v['id'];
                $where1['status'] = 1;
                $child = Db::name("system_article_classify")
                    ->where($where1)->field("id,name,type,thumb")
                    ->order("sort desc,id desc")
                    ->select();
                if(!empty($child)){
                    $list[$k]['child'] = $child;
                }
            }
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['data'] = $list;
            return json_encode($black);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = "授权失败";
            return json_encode($black);
        }
    }

    //获取子栏目
    public function childApi(){
        try {
            $where['pid'] = $_POST['id'];
            $list = Db::name("system_article_classify")
                ->where($where)->field("id,name,type")
                ->order("sort desc,id desc")
                ->limit(4)->select();
            //获取下级栏目
            foreach ($list as $k=>$v){
                $where1['pid'] = $v['id'];
                $child = Db::name("system_article_classify")
                    ->where($where1)->field("id,name,type")
                    ->order("sort desc,id desc")
                    ->limit(4)->select();
                if(!empty($child)){
                    $list[$k]['child'] = $child;
                }
            }
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['data'] = $list;
            return json_encode($black);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
            return json_encode($black);
        }
    }

    //获取通知公告
    public function notice(){
        try{
            //查询公告栏目
            $where['colony_id'] = $_POST['colony_id'];
            $where['name'] = ['like',"%公告%"];
            $classify_id = Db::name("system_article_classify")->where($where)->value("id");
            $type = Db::name("system_article_classify")->where($where)->value("type");
            $list = Db::name("admin_content")
                ->where(['classify_id'=>$classify_id])
                ->field("id,title")
                ->order("id desc")
                ->limit(5)->select();
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['list'] = $list;
            $black['type'] = $type;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取栏目下的内容
    public function article(){
        try {
            $limit = empty($_POST['limit'])?10:intval($_POST['limit']);
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $start = ($page-1)*$limit;
            $where['classify_id'] =$_POST['classify_id'];
            $type = Db::name("system_article_classify")->where(['id'=>$_POST['classify_id']])->value("type");

            $preg = "/^http(s)?:\\/\\/.+/";
           switch ($type){
               case "2":
                   $where['status'] = 2;
                   $list = Db::name("system_record")
                       ->where($where)
                       ->field("id,name,faceUrl,branch")
                       ->order("sort desc,id desc")
                       ->limit($start,$limit)->select();
                   $count = Db::name("system_record")->where($where)->count("*");
                   foreach ($list as $k => $v) {
                       if(!empty($v['faceUrl']) && $v['faceUrl']!=''){
                           if(!preg_match($preg,$v['faceUrl']))
                           {
                               $list[$k]['faceUrl'] = "https://" . $_SERVER['HTTP_HOST'] . $v['faceUrl'];
                           }
                       }
                       //$list[$k]['faceUrl'] = empty($v['faceUrl']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" : "https://" . $_SERVER['HTTP_HOST'] . $v['faceUrl'];
                   }
                   break;
               case "10":
                    $where['status'] = 1;
                    $list = Db::name("system_exchange")->where($where)->field("id,title,thumb,integral")->order("sort desc,id desc")->select();
                    $count = Db::name("system_exchange")->where($where)->order("sort desc,id desc")->limit($start,$limit)->count("*");
                   foreach ($list as $k => $v) {
                       if(!empty($v['thumb']) && $v['thumb']!=''){
                           if(!preg_match($preg,$v['thumb']))
                           {
                               $list[$k]['thumb'] = "https://" . $_SERVER['HTTP_HOST'] . $v['thumb'];
                           }
                       }
                   }
                   break;
               default:
                   $where['status'] = 1;
                   $list = Db::name("admin_content")
                       ->where($where)
                       ->order("sort desc,id desc")
                       ->limit($start,$limit)->select();
                   $count = Db::name("admin_content")->where($where)->count("*");
                   foreach ($list as $k => $v) {
                       if(!empty($v['thumb'])  && $v['thumb']!=''){
                           if(!preg_match($preg,$v['thumb']))
                           {
                               $list[$k]['thumb'] = "https://" . $_SERVER['HTTP_HOST'] . $v['thumb'];
                           }
                       }
                       //$list[$k]['thumb'] = empty($v['thumb']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" : "https://" . $_SERVER['HTTP_HOST'] . $v['thumb'];
                   }
                   break;
           }
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['list'] = $list;
            $black['count'] = $count;
            $black['page'] = $page;
            return json_encode($black);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = "获取失败";
            return json_encode($black);
        }
    }

    //获取积分商品详细信息
    public function exchangeApi(){
        try{
            $id = $_POST['id'];
            $where['id'] = $id;
            $data = Db::name("system_exchange")->field("id,title,thumb,integral,images,content,type,inventory")->where($where)->find();
            $data['images'] = explode(";",$data['images']);
            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = "获取失败";
        }
        return json_encode($black);

    }

    //根据小区获取楼栋信息
    public function storiedList(){
        try{
            $community_id = $_POST['community_id'];
            //获取小区下面的楼栋信息
            $data = Db::name("system_community_building")
                ->where(['community_id'=>$community_id])
                ->field("id,buildingName")->select();

            //获取楼栋下的单元及楼层
            foreach ($data as $k=>$v){
                $element = Db::name("system_community_element")->where(['building_id'=>$v['id']])->field("id,elementName,floor")->select();
                $floor = 0;
                foreach ($element as $k1=>$v1){
                    if($v1['floor']>$floor){
                        $floor = $v1['floor'];
                    }
                    unset($element[$k1]['floor']);
                }
                $data[$k]['floor'] = $floor;
                $data[$k]['element'] = $element;
            }
            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = "获取失败";
        }
        return json_encode($black);
    }


    //根据单元获取党员列表
    public function recordList(){
        try{
            $limit = empty($_POST['limit'])?10:intval($_POST['limit']);
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $start = ($page-1)*$limit;
            $where = [];
            $where['b.element_id'] = $_POST['element_id'];
            $list = Db::name("system_record")->alias("a")
                ->join("system_community_housing b","a.housing_id = b.id")
                ->join("system_community_element c","b.element_id = c.id")
                ->where($where)
                ->field("a.id,a.name,a.faceUrl,a.branch")
                ->select();
            $count = Db::name("system_record")->alias("a")
                ->join("system_community_housing b","a.housing_id = b.id")
                ->join("system_community_element c","b.element_id = c.id")
                ->where($where)->count("*");

            $preg = "/^http(s)?:\\/\\/.+/";
            foreach ($list as $k => $v) {
                if(!empty($v['faceUrl'])){
                    if(!preg_match($preg,$v['faceUrl']))
                    {
                        $list[$k]['faceUrl'] = "https://" . $_SERVER['HTTP_HOST'] . $v['faceUrl'];
                    }
                }

            }
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['list'] = $list;
            $black['count'] = $count;
            $black['page'] = $page;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = "获取失败";
        }
        return json_encode($black);
    }

    //获取党员详细信息
    public function recordContent(){
        try {
            //性别
            $sex = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
            $sex = explode("|", $sex);
            foreach ($sex as $k => $v) {
                $sex[$k + 1] = $v;
            }
            unset($sex[0]);

            //学历
            $schooling = Db::name("config_attribute")->where(['key' => "schooling"])->value("value");
            $schooling = explode("|", $schooling);
            foreach ($schooling as $k => $v) {
                $schooling[$k + 1] = $v;
            }
            unset($schooling[0]);

            //党员身份
            $account = Config::get("account");
            $where['id'] = $_POST['id'];
            $data = Db::name("system_record")->where($where)->find();

            $housing = Db::name("system_community_housing")->where(['id'=>$data['housing_id']])->find();
            $propertyName = Db::name("system_community")->where(['id'=>$data['community_id']])->value("propertyName");
            $buildingName = Db::name("system_community_building")->where(['id'=>$housing['building_id']])->value("buildingName");
            $elementName = Db::name("system_community_element")->where(['id'=>$housing['element_id']])->value("elementName");
            $data['propertyName'] = $propertyName;
            $data['buildingName'] = $buildingName;
            $data['elementName'] = $elementName;
            $data['roomNumber'] = $housing['roomNumber'];
            $data['sex'] = $sex[$data['sex']];
            $data['schooling'] = $schooling[$data['schooling']];
            $data['account'] = $account[$data['account']];
            $preg = "/^http(s)?:\\/\\/.+/";
            if(!empty($data)){

                if(!empty($data['faceUrl'])){
                    if(!preg_match($preg,$data['faceUrl']))
                    {
                        $data['faceUrl'] = "https://" . $_SERVER['HTTP_HOST'] . $data['faceUrl'];
                    }
                }

                //$data['faceUrl'] = empty($data['faceUrl']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" : "https://" . $_SERVER['HTTP_HOST'] . $data['faceUrl'];

                if(empty($data['path']) && $data['path']!=''){
                    if(!preg_match($preg,$data['path']))
                    {
                        $data['path'] = "https://" . $_SERVER['HTTP_HOST'] .$data['path'];
                    }
                }
            }

            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['data'] = $data;
            return json_encode($black);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = "获取失败";
            return json_encode($black);
        }
    }


    //获取文章内容
    public function contetn(){
        try {
            $where['id'] = $_POST['id'];
            $data = Db::name("admin_content")->where($where)->find();

            $preg = "/^http(s)?:\\/\\/.+/";
            if(!empty($data)){

                if(!empty($data['thumb'])){
                    if(!preg_match($preg,$data['thumb']) && $data['thumb']!='')
                    {
                        $data['thumb'] = "https://" . $_SERVER['HTTP_HOST'] . $data['thumb'];
                    }
                }

                //$data['thumb'] = empty($data['thumb']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" : "https://" . $_SERVER['HTTP_HOST'] . $data['thumb'];
            }


            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['data'] = $data;
            return json_encode($black);
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = "获取失败";
            return json_encode($black);
        }
    }

    //根据社区获取小区
    public function community(){
        try{

            $colony_id = $_POST['colony_id'];
            //获取区域id
            $list = Db::name("system_colony_community")->alias("a")
                ->join("system_community b","a.community_id = b.id")
                ->where(['a.colony_id'=>$colony_id])
                ->field("b.id,b.propertyName")
                ->select();

            $black['code'] = 0;
            $black['data'] = $list;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //添加志愿者下拉选项
    public function volunteersSelect(){
        //性别
        $sex1 = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
        $sex1 = explode("|", $sex1);
        $sex[] = "请选择";
        foreach ($sex1 as $k => $v) {
            $sex[] = $v;
        }

        //身体状况
        $condition1 = Db::name("config_attribute")->where(['key' => "condition"])->value("value");
        $condition1 = explode("|", $condition1);
        $condition[] = "请选择";
        foreach ($condition1 as $k => $v) {
            $condition[$k + 1] = $v;
        }

        //学历
        $schooling1 = Db::name("config_attribute")->where(['key' => "schooling"])->value("value");
        $schooling1 = explode("|", $schooling1);
        $schooling[] = "请选择";
        foreach ($schooling1 as $k => $v) {
            $schooling[$k + 1] = $v;
        }

        //义工类型
        $volunteers1 = Db::name("config_attribute")->where(['key' => "volunteers"])->value("value");
        $volunteers1 = explode("|", $volunteers1);
        $volunteers[] = "请选择";
        foreach ($volunteers1 as $k => $v) {
            $volunteers[$k + 1] = $v;
        }

        //政治面貌
        $political_outlook1 = Db::name("config_attribute")->where(['key' => "political_outlook"])->value("value");
        $political_outlook1 = explode("|", $political_outlook1);
        $political_outlook[] = "请选择";
        foreach ($political_outlook1 as $k => $v) {
            $political_outlook[$k + 1] = $v;
        }

        $assign = [
            "sex" => $sex,
            "condition" => $condition,
            "schooling" => $schooling,
            "volunteers" => $volunteers,
            "political_outlook"=>$political_outlook
        ];

        $black = $assign;
        return json_encode($black);
    }


    //完善志愿者信息
    public function volunteers(){
        $common = new Common();
        $unionid = $common->common();
        try{
            $where1['id'] = $unionid;
            $fans = Db::name("system_fans")->where($where1)->find();
            if(empty($fans)){
                throw new Exception("粉丝信息有误");
            }
            $fans_id = $fans['id'];

            $rule = [
//                ["colony_id", "require", "请选择所属社区"],
//                ["community", "require", "请选择小区"],
//                ["building", "require", "请选择栋座"],
//                ["element", "require", "请选择单元"],
//                ["floor", "require", "请选择楼层"],
//                ["room", "require", "请选择房号"],
                ["name", "require", "请输入姓名"],
//                ["place", "require", "请输入所在籍贯"],
                ["idcard", "require", "请输入身份证号"],
                ["work_units", "require", "请输入工作单位"],
                ["mobile", "require", "请输入联系方式"],
//                ["weight", "require", "请输入体重"],
//                ["height", "require", "请输入身高"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();

            //测试数据
            /*$value = [
                'colony_id'=>3,'community'=>44,'building'=>9,'element'=>18,'floor'=>12,'room'=>4,
                'name'=>'支雪冬','place'=>'湖北','idcard'=>'42058219921108359X','work_units'=>'务农','mobile'=>'15578984612','weight'=>'60','height'=>'170','remark'=>'备注',
                'sex'=>'1','condition'=>'1','schooling'=>'1','volunteers'=>'1'
            ];*/


            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }


            //获取房源id
//            $housingWhere['community_id'] = $value['community'];
//            $housingWhere['building_id'] = $value['building'];
//            $housingWhere['element_id'] = $value['element'];
//            $housingWhere['floor'] = intval($value['floor']);
//            $housingWhere['room'] = intval($value['room']);
//            $housing = Db::name("system_community_housing")->where($housingWhere)->find();
//            if (!empty($housing)) {
//                $value['housing_id'] = $housing['id'];
//                $value['community_id'] = $value['community'];
//                //删除多余数变量
//                unset($value['community']);
//                unset($value['building']);
//                unset($value['element']);
//                unset($value['floor']);
//                unset($value['room']);
//            } else {
//                throw new Exception("房源不存在");
//            }

            if (!empty($value['idcard'])) {
                if (!$common->setCard($value['idcard'])) {
                    throw new Exception("身份证号格式有误");
                }
            }
            if (!empty($value['mobile'])) {
                if (!$common->isMobile($value['mobile'])) {
                    throw new Exception("手机号格式有误");
                }
            }
            $where['fans_id'] = $fans_id;
            $where['colony_id'] = $value['colony_id'];
            $registration = Db::name("system_registration")->where($where)->value("id");

            $value['fans_id'] = $fans_id;
            if(empty($registration)){
                $value['addtime'] = time();
                Db::name("system_registration")->insertGetId($value);
                $log['remark'] = "市民【" . $fans['nickname'] . "】添加志愿者信息";
            }else{
                throw new Exception("您已注册志愿者，请勿重复注册");
//                $value['updatetime'] = time();
//                Db::name("system_registration")->where($where)->update($value);
//                $log['remark'] = "市民【" . $fans['nickname'] . "】更新志愿者信息";
            }

            $log['controller'] = "Colony";
            $log['methods'] = "volunteers";
            $log['operator'] = $unionid;
            $log['operator_id'] = $fans_id;
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //活动报名
    public function signUp(){
        $common = new Common();
        $userid = $common->common();
        try{
            $where['id'] = $userid;
            $fans = Db::name("system_fans")->where($where)->find();
            if(empty($fans)){
                throw new Exception("粉丝信息有误");
            }
            $fans_id = $fans['id'];

            $rule = [
                ["id", "require", "请选择要参加的活动"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }

            //获取活动详细信息
            $content = Db::name("admin_content")->where(['id'=>$data['id']])->field("id,classify_id,title,start_time,end_time")->find();
            if(empty($content)){
                throw new Exception("活动不存在");
            }
            if(time()<$content['start_time']){
                throw new Exception("活动暂未开始");
            }
            if(time()>$content['end_time']){
                throw new Exception("活动已结束");
            }

            //获取社区id
            $classify = Db::name("system_article_classify")->where(['id'=>$content['classify_id']])->field("type,colony_id")->find();

            //获取志愿者身份
            $registration = Db::name("system_registration")->where(['fans_id'=>$fans_id,'colony_id'=>$classify['colony_id']])->value("id");
            if(empty($registration)){
                throw new Exception("请先完善志愿者信息");
            }

            if(!empty($data['images'])){
                $images = $data['images'];
                $user['images'] = implode($images,",");
            }
            unset($data['images']);

            $user['content_id'] = $content['id'];
            $user['fans_id'] = $registration;

            $where2['content_id'] = $content['id'];
            $where2['fans_id'] = $registration;
            $detail = Db::name("admin_content_user")->where($where2)->find();
            if(empty($detail)){
                $user['addtime'] = time();
                $signid = Db::name("admin_content_user")->insertGetId($user);
            }else{
                $user['updatetime'] = time();
                $signid = $detail['id'];
                Db::name("admin_content_user")->where($where2)->update($user);
            }

            $log['remark'] = "市民【" .$fans['nickname'] . "】参加活动,,活动id【".$content['id']."】，报名单id【".$signid."】";
            $log['controller'] = "Colony";
            $log['methods'] = "signUp";
            $log['operator'] = $userid;
            $log['operator_id'] = $fans_id;
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //队伍列表
    public function teamList(){
        $common = new Common();
        $userid = $common->common();
        try{
            //队伍id
            $id = empty($_POST['id'])?0:$_POST['id'];

            $sex = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
            $sex = explode("|", $sex);
            foreach ($sex as $k => $v) {
                $sex[$k + 1] = $v;
            }
            unset($sex[0]);

            $where['a.content_id'] = $id;
            $list = Db::name("admin_content_user")->alias("a")
                ->join("system_registration b", "a.fans_id = b.id")
                ->where($where)->field("a.id,b.name,b.sex,b.mobile,a.addtime")->select();
            foreach ($list as $k => $v) {
                $list[$k]['sex'] = $sex[$v['sex']];
                $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
            }

            $black['list'] = $list;
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //加入队伍
    public function joinTeam(){
        $common = new Common();
        $userid = $common->common();
        try{
            $where['id'] = $userid;
            $fans = Db::name("system_fans")->where($where)->find();
            if(empty($fans)){
                throw new Exception("粉丝信息有误");
            }
            $fans_id = $fans['id'];

            $rule = [
                ["id", "require", "请选择要参加的活动"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }

            //获取活动详细信息
            $content = Db::name("admin_content")->where(['id'=>$data['id']])->field("id,classify_id,title,start_time,end_time")->find();
            if(empty($content)){
                throw new Exception("该队伍不存在");
            }


            //获取社区id
            $classify = Db::name("system_article_classify")->where(['id'=>$content['classify_id']])->field("type,colony_id")->find();

            //获取志愿者身份
            $registration = Db::name("system_registration")->where(['fans_id'=>$fans_id,'colony_id'=>$classify['colony_id']])->value("id");
            if(empty($registration)){
                throw new Exception("请先完善志愿者信息");
            }

            $user['content_id'] = $content['id'];
            $user['fans_id'] = $registration;

            $where2['content_id'] = $content['id'];
            $where2['fans_id'] = $registration;
            $detail = Db::name("admin_content_user")->where($where2)->find();
            if(empty($detail)){
                $user['addtime'] = time();
                $signid = Db::name("admin_content_user")->insertGetId($user);
            }else{
                $user['updatetime'] = time();
                $signid = $detail['id'];
                Db::name("admin_content_user")->where($where2)->update($user);
            }

            $log['remark'] = "市民【" .$fans['nickname'] . "】加入队伍,,队伍id【".$content['id']."】，关联id【".$signid."】";
            $log['controller'] = "Colony";
            $log['methods'] = "joinTeam";
            $log['operator'] = $userid;
            $log['operator_id'] = $fans_id;
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //我参加的活动
    public function activity(){
        $common = new Common();
        $userid = $common->common();

        try{
            $where1['id'] = $userid;
            $fans = Db::name("system_fans")->where($where1)->find();
            if(empty($fans)){
                throw new Exception("粉丝信息有误");
            }
            $fans_id = $fans['id'];

            if(empty($_POST['colony_id'])){
                throw new Exception("请选择所在社区");
            }
            $colony_id = $_POST['colony_id'];

            $limit = empty($_POST['limit'])?10:intval($_POST['limit']);
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $start = ($page-1)*$limit;

            //获取志愿者身份
            $registration = Db::name("system_registration")->where(['fans_id'=>$fans_id,'colony_id'=>$colony_id])->value("id");
            if(empty($registration)){
                throw new Exception("请先完善志愿者信息");
            }

            $where['a.fans_id'] = $registration;
            $where['c.colony_id'] = $_POST['colony_id'];
            $where['c.type'] = ['neq',9];
            $lists = Db::name("admin_content_user")->alias("a")
                ->join("admin_content b","a.content_id = b.id")
                ->join("system_article_classify c","b.classify_id = c.id")
                ->where($where)
                ->field("b.title,a.addtime")
                ->limit($start,$limit)->select();

            foreach ($lists as $k=>$v){
                $lists[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
            }
            $count = Db::name("admin_content_user")->alias("a")
                ->join("admin_content b","a.content_id = b.id")
                ->join("system_article_classify c","b.classify_id = c.id")
                ->where($where)
                ->count("*");
            $total = ceil($count/$limit);

            $black['code'] = 0;
            $black['data'] = $lists;
            $black['count'] = $count;
            $black['total'] = $total;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


}